package com.stu.day17;

/**
 * @author zy
 * @date 2025/2/17 14:11
 */
public class RunnableCreateDemo implements Runnable {
    int ticket = 10;
    @Override
    public void run() {
        for(int i = ticket; i >= 0; i--) {
            //Thread.currentThread()当前线程对象
            System.out.println(Thread.currentThread().getName()+"剩余票数: "+ticket--);

            try {
                Thread.sleep(400);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
class RunnableTest{
    public static void main(String[] args) {
        //Thread(Runnable r);
        //Thread(Runnable r,String name);
        Runnable r1 = new RunnableCreateDemo();  // t1,t2都根据r1创建对象的话,共享非静态成员
        //Runnable r2 = new RunnableCrateDemo();

        //1. 分别创建俩个对象
        // 共享非静态的ticket
        Thread t1 = new Thread(r1,"线程one");
        //Thread t2 = new Thread(r1,"线程two");
        Thread t2 = new Thread(r1,"线程two");

        Thread t3 = new Thread(()->{  // 与t1,t2依据不同的Runnable创建对象
            int ticket = 10;
            for(int i = ticket; i >= 0; i--) {
                //Thread.currentThread()当前线程对象
                System.out.println(Thread.currentThread().getName()+"剩余票数: "+ticket--);

                try {
                    Thread.sleep(400);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"线程three");

        t1.start();
//        t2.start();
        t3.start();
    }
}